package com.gimi.cloud.bbp.manage.service.mall.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.mall.MallAppSpecialGroupDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.mall.MallAppSpecialDTO;
import com.gimi.cloud.bbp.dto.mall.MallAppSpecialGroupDTO;
import com.gimi.cloud.bbp.entity.mall.MallAppSpecialGroupEntity;
import com.gimi.cloud.bbp.manage.service.mall.MallAppSpecialGroupService;
import com.gimi.cloud.bbp.manage.service.mall.MallAppSpecialService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 专场组 Service实现类
 * @author chendi
 * @date 2018年1月31日
 */
@Service
public class MallAppSpecialGroupServiceImpl implements MallAppSpecialGroupService {
    private static final Logger logger = LoggerFactory.getLogger(MallAppSpecialGroupServiceImpl.class);
    private MallAppSpecialGroupDao mallAppSpecialGroupDao;
    private MallAppSpecialService mallAppSpecialService;

    @Autowired
    public MallAppSpecialGroupServiceImpl(MallAppSpecialGroupDao mallAppSpecialGroupDao,MallAppSpecialService mallAppSpecialService) {
        this.mallAppSpecialGroupDao = mallAppSpecialGroupDao;
        this.mallAppSpecialService = mallAppSpecialService;
    }

    @Override
    public List<MallAppSpecialGroupDTO> queryList(MallAppSpecialGroupDTO dto, boolean isPaging) throws Exception {
        if (isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "SELECT gro.* FROM mall_app_special_group gro WHERE gro.is_delete = 0 AND gro.asg_name like '%'||:asgName||'%' AND gro.asg_status =:asgStatus  ORDER BY asg_status,asg_predict_start_time ";
        QueryParam param = new QueryParam();
        param.setSql(sql);
        param.setParamObject(dto);
        return mallAppSpecialGroupDao.selectListDTOBySql(param);
    }

    @Override
    public MallAppSpecialGroupDTO load(MallAppSpecialGroupDTO dto, Object id) throws Exception {
        MallAppSpecialGroupEntity specialEntity = mallAppSpecialGroupDao.queryBy("asg_id",id);
        if(null == specialEntity){
            return null;
        }
        MallAppSpecialGroupDTO specialDTO = new MallAppSpecialGroupDTO();
        ReflectUtils.copyPropertiesIgnoreNull(specialEntity,specialDTO);
        return specialDTO;
    }

    @Override
    public MallAppSpecialGroupDTO saveOrUpdate(MallAppSpecialGroupDTO dto) throws Exception {
        MallAppSpecialGroupEntity groupEntity = null;
        if (null != dto.getAsgId()) {
            groupEntity = mallAppSpecialGroupDao.queryBy("asg_id", dto.getAsgId());
            if (null == groupEntity) {
            	logger.info("未获取specialid为" + dto.getAsgId() + "的special信息，更新失败");
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取specialid为" + dto.getAsgId() + "的special信息，更新失败");
            }
        } else {
            groupEntity = new MallAppSpecialGroupEntity();
        }
        ReflectUtils.copyPropertiesIgnoreNull(dto, groupEntity);
        if (null != dto.getAsgId()) {
            mallAppSpecialGroupDao.baseUpdateByPrimaryKey(groupEntity);
        } else {
            mallAppSpecialGroupDao.baseInsert(groupEntity);
        }
        if(null == groupEntity.getAsgId()){
            throw new MsgException(ResponseCodeEnum.NO_DATA,"系统异常，保存失败");
        }
        List<Integer> specialItem = new ArrayList<>();
        if(null != dto.getSpecials() && dto.getSpecials().size() > 0){
            for(MallAppSpecialDTO specialDTO:dto.getSpecials()){
                //专场组id
                specialDTO.setAsgId(groupEntity.getAsgId());
                specialDTO.setUid(dto.getUid());
                specialDTO.setUname(dto.getUname());
                //保存关联专场
                mallAppSpecialService.saveOrUpdate(specialDTO);
                specialItem.add(specialDTO.getAslId());
            }
        }
        MallAppSpecialDTO special = new MallAppSpecialDTO();
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,special);
        mallAppSpecialService.deleteBannerByIds(special,specialItem,groupEntity.getAsgId());
        ReflectUtils.copyPropertiesIgnoreNull(groupEntity, dto);
        return dto;
    }

    @Override
    public int delete(MallAppSpecialGroupDTO dto, Object id, boolean realDel) throws Exception {
        MallAppSpecialGroupEntity groupEntity = mallAppSpecialGroupDao.queryBy("asg_id", id);
        if (null == groupEntity) {
            throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取specialid为" + id + "的special信息，删除失败");
        }
        //非物理删除
        if(!realDel){
            ReflectUtils.copyPropertiesIgnoreNull(dto,groupEntity);
            groupEntity.setIsDelete(1);
            return mallAppSpecialGroupDao.baseUpdateByPrimaryKey(groupEntity);
        }
        return mallAppSpecialGroupDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<MallAppSpecialGroupDTO> queryAppSpecialGroupAndInitChild(MallAppSpecialGroupDTO dto, boolean isPaging) throws Exception {
        if (isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "SELECT gro.* FROM mall_app_special_group gro WHERE gro.is_delete = 0 AND gro.asg_name like '%'||:asgName||'%' AND gro.asg_status =:asgStatus  ORDER BY asg_status,asg_predict_start_time ";
        QueryParam param = new QueryParam();
        param.setSql(sql);
        param.setParamObject(dto);
        List<MallAppSpecialGroupDTO> appSpecialGroupList =  mallAppSpecialGroupDao.selectListDTOBySql(param);
        if(null != appSpecialGroupList && appSpecialGroupList.size() > 0){
            MallAppSpecialDTO appSpecial = new MallAppSpecialDTO();
            for(MallAppSpecialGroupDTO appSpecialGroup:appSpecialGroupList){
                appSpecial.setAsgId(appSpecialGroup.getAsgId());
                appSpecialGroup.setSpecials(mallAppSpecialService.queryList(appSpecial,false));
            }
        }
        return appSpecialGroupList;
    }
}
